<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button type="success" @click="isdr">新增</el-button>
        <el-button type="warning" @click="updateUser">修改</el-button>
        <el-button type="danger" @click="delUser">删除</el-button>
      </el-col>
      <el-col :span="12">
        <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
          <el-select v-model="select" style="width:130px" slot="prepend" placeholder="请选择">
            <el-option label="姓名" value="name"></el-option>
          </el-select>
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <!--  -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column fixed type="selection" width="70"></el-table-column>
      <el-table-column type="index" width="70" label="#"></el-table-column>
      <el-table-column prop="title" label="标题" width="180"></el-table-column>
      <el-table-column prop="author" label="作者" width="160"></el-table-column>
      <el-table-column prop="type" label="类别" width="160"></el-table-column>
      <el-table-column prop="time" label="用时" width="160"></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row,'删除')" type="text" size="small">删除</el-button>
          <el-button @click="handleClick(scope.row,'修改')" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
    ></el-pagination>
    <!--  -->
    <el-drawer :title="title" :visible.sync="drawer" :before-close="handleClose">
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-form-item label="标题" :label-width="formLabelWidth">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="作者" :label-width="formLabelWidth">
            <el-input v-model="form.author" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类别" :label-width="formLabelWidth">
            <el-input v-model="form.cid" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用时" :label-width="formLabelWidth">
            <el-input v-model="form.time" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div>
          <mavon-editor v-model="form.content" />
        </div>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm('取消')">取 消</el-button>
          <el-button type="primary" @click="cancelForm('确定')">确 定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      title: "新增",
      drawer: false,
      multipleSelection: [],
      input: "",
      select: "",
      form: {},
      formLabelWidth: "80px",
      pageSize: 10,
      currentPage: 1
    };
  },
  mounted() {
    this.GetUser();
  },
  methods: {
    ...mapActions("msbd", ["getData", "addData", "editData", "delData"]),
    isdr() {
      this.form = {};
      this.drawer = true;
      this.title = "新增";
      console.log(this.form);
    },
    GetUser() {
      this.getData({
        pageSize: this.pageSize,
        page: this.currentPage - 1,
        sortBy: "id",
        order: "desc"
      });
    },
    updateUser() {
      if (this.multipleSelection.length != 1) {
        return;
      } else {
        let row = this.multipleSelection[0];
        this.form = row;
        this.drawer = true;
        this.title = "修改";
      }
    },
    delUser() {
      if (this.multipleSelection.length == 0) {
        alert("选择删除项");
        return;
      }
      let ids = this.multipleSelection.map(item => {
        return item.id;
      });
      this.delete(ids.join(","));
      this.GetUser();
    },
    delete(id) {
      this.delData(id);
    },
    handleClick(row, v) {
      console.log(row, v);
      if (v == "修改") {
        this.form = row;
        this.drawer = true;
        this.title = "修改";
      } else {
        console.log(row.id);
        this.delete(row.id);
      }
      this.GetUser();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      // console.log(val);
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.GetUser();
      // console.log(`当前页: ${val}`);
    },
    handleClose(done) {
      (this.form = {}), (this.title = "新增");
      done();
    },
    async cancelForm(v) {
      let row = this.form;
      if (v == "确定") {
        if (this.title == "修改") {
          await this.editData(this.form);
        } else {
          await this.addData(row);
        }
        this.GetUser();
        this.form = {};
      }
      this.drawer = false;
    }
  },
  computed: {
    ...mapState("msbd", ["data"]),
    tableData() {
      return this.data;
    }
  }
};
</script>

<style scoped>
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>